home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / spambayes / i18n.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  5.3 KB  |  189 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Internationalisation
  5.  
  6. Classes:
  7.     LanguageManager - Interface class for languages.
  8.  
  9. Abstract:
  10.  
  11. Manages the internationalisation (i18n) aspects of SpamBayes.
  12. '''
  13. __author__ = 'Hernan Martinez Foffani <hfoffani@yahoo.com>'
  14. __credits__ = 'Tony Meyer, All the SpamBayes folk.'
  15.  
  16. try:
  17.     (True, False)
  18. except NameError:
  19.     (True, False) = (1, 0)
  20.  
  21. import os
  22. import sys
  23. from locale import getdefaultlocale
  24. from gettext import translation, NullTranslations
  25. if hasattr(sys, 'frozen'):
  26.     if sys.frozen == 'dll':
  27.         import win32api
  28.         this_filename = win32api.GetModuleFileName(sys.frozendllhandle)
  29.     else:
  30.         this_filename = __file__
  31.     LC_DIR = os.path.dirname(os.path.dirname(this_filename))
  32.     DIALOGS_DIR = os.path.join(os.path.dirname(__file__), 'languages')
  33. else:
  34.     
  35.     try:
  36.         this_filename = os.path.abspath(__file__)
  37.     except NameError:
  38.         this_filename = os.path.abspath(sys.argv[0])
  39.  
  40.     LC_DIR = os.path.dirname(this_filename)
  41.     DIALOGS_DIR = os.path.join(LC_DIR, 'languages')
  42.  
  43. class LanguageManager:
  44.     
  45.     def __init__(self):
  46.         self.current_langs_codes = []
  47.         self._sys_path_modifications = []
  48.  
  49.     
  50.     def set_language(self, lang_code = None):
  51.         '''Set a language as the current one.'''
  52.         if not lang_code:
  53.             return None
  54.         
  55.         self.current_langs_codes = [
  56.             lang_code]
  57.         self._rebuild_syspath_for_dialogs()
  58.         self._install_gettext()
  59.  
  60.     
  61.     def locale_default_lang(self):
  62.         '''Get the default language for the locale.'''
  63.         return getdefaultlocale()[0]
  64.  
  65.     
  66.     def add_language(self, lang_code = None):
  67.         '''Add a language to the current languages list.
  68.  
  69.         The list acts as a fallback mechanism, where the first language of
  70.         the list is used if possible, and if not the second one, and so on.
  71.         '''
  72.         if not lang_code:
  73.             return None
  74.         
  75.         self.current_langs_codes.insert(0, lang_code)
  76.         self._rebuild_syspath_for_dialogs()
  77.         self._install_gettext()
  78.  
  79.     
  80.     def clear_language(self):
  81.         '''Clear the current language(s) and set SpamBayes to use
  82.         the default.'''
  83.         self.current_langs_codes = []
  84.         self._clear_syspath()
  85.         lang = NullTranslations()
  86.         lang.install()
  87.  
  88.     
  89.     def import_ui_html(self):
  90.         '''Load and return the appropriate ui_html.py module for the
  91.         current language.'''
  92.         for language in self.current_langs_codes:
  93.             moduleName = 'spambayes.languages.%s.i18n_ui_html' % (language,)
  94.             
  95.             try:
  96.                 module = __import__(moduleName, { }, { }, ('spambayes.languages', language))
  97.             except ImportError:
  98.                 continue
  99.  
  100.             return module
  101.         
  102.         ui_html = ui_html
  103.         import spambayes.resources
  104.         return ui_html
  105.  
  106.     
  107.     def _install_gettext(self):
  108.         '''Set the gettext specific environment.'''
  109.         lang = translation('messages', LC_DIR, self.current_langs_codes, fallback = True)
  110.         lang.install()
  111.  
  112.     
  113.     def _rebuild_syspath_for_dialogs(self):
  114.         '''Add to sys.path the directories of the translated dialogs.
  115.  
  116.         For each language of the current list, we add two directories,
  117.         one for language code and country and the other for the language
  118.         code only, so we can simulate the fallback procedures.'''
  119.         self._clear_syspath()
  120.         for lcode in self.current_langs_codes:
  121.             code_and_country = os.path.join(DIALOGS_DIR, lcode, 'DIALOGS')
  122.             code_only = os.path.join(DIALOGS_DIR, lcode.split('_')[0], 'DIALOGS')
  123.             if code_and_country not in sys.path:
  124.                 sys.path.append(code_and_country)
  125.                 self._sys_path_modifications.append(code_and_country)
  126.             
  127.             if code_only not in sys.path:
  128.                 sys.path.append(code_only)
  129.                 self._sys_path_modifications.append(code_only)
  130.                 continue
  131.         
  132.  
  133.     
  134.     def _clear_syspath(self):
  135.         '''Clean sys.path of the stuff that we put in it.'''
  136.         for path in self._sys_path_modifications:
  137.             sys.path.remove(path)
  138.         
  139.         self._sys_path_modifications = []
  140.  
  141.  
  142.  
  143. def test():
  144.     lm = LanguageManager()
  145.     print 'INIT: len(sys.path): ', len(sys.path)
  146.     print 'TEST default lang'
  147.     lm.set_language(lm.locale_default_lang())
  148.     print '\tCurrent Languages: ', lm.current_langs_codes
  149.     print '\tlen(sys.path): ', len(sys.path)
  150.     print '\t', _('Help')
  151.     print 'TEST clear_language'
  152.     lm.clear_language()
  153.     print '\tCurrent Languages: ', lm.current_langs_codes
  154.     print '\tlen(sys.path): ', len(sys.path)
  155.     print '\t', _('Help')
  156.     print 'TEST set_language'
  157.     for langcode in [
  158.         'kk_KK',
  159.         'z',
  160.         '',
  161.         'es',
  162.         None,
  163.         'es_AR']:
  164.         print 'lang: ', langcode
  165.         lm.set_language(langcode)
  166.         print '\tCurrent Languages: ', lm.current_langs_codes
  167.         print '\tlen(sys.path): ', len(sys.path)
  168.         print '\t', _('Help')
  169.     
  170.     lm.clear_language()
  171.     print 'TEST add_language'
  172.     for langcode in [
  173.         'kk_KK',
  174.         'z',
  175.         '',
  176.         'es',
  177.         None,
  178.         'es_AR']:
  179.         print 'lang: ', langcode
  180.         lm.add_language(langcode)
  181.         print '\tCurrent Languages: ', lm.current_langs_codes
  182.         print '\tlen(sys.path): ', len(sys.path)
  183.         print '\t', _('Help')
  184.     
  185.  
  186. if __name__ == '__main__':
  187.     test()
  188.  
  189.